package com.travel.server.manager.impl.command.customer;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.server.manager.dto.request.customer.QueryCustomerInfoDetailRequest;
import com.travel.server.manager.dto.response.customer.QueryCustomerInfoDetailResponse;
import com.travel.server.manager.dto.response.customer.QueryCustomerInfoDetailResponse.CustomerTagItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.*;
import com.travel.server.manager.impl.dal.CustomerTagDal.CustomerTagParam;
import com.travel.server.manager.impl.dal.TagDal.TagParam;
import com.travel.server.manager.model.CustomerInfo;
import com.travel.server.manager.model.CustomerTagInfo;
import com.travel.server.manager.model.MemberGradeInfo;
import com.travel.server.manager.model.TagInfo;
import com.travel.server.manager.model.dto.CustomerOrderDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 18:02
 * @Desc:
 */
@Component
@Slf4j
public class QueryCustomerInfoDetailCommand extends AbstractCommand<QueryCustomerInfoDetailRequest, QueryCustomerInfoDetailResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Autowired
    private MemberGradeDal memberGradeDal;

    @Autowired
    private TagDal tagDal;

    @Autowired
    private CustomerTagDal customerTagDal;

    @Autowired
    private OrderDal orderDal;

    @Override
    protected void checkInput(QueryCustomerInfoDetailRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryCustomerInfoDetailResponse handle(QueryCustomerInfoDetailRequest request) {
        checkInput(request);
        QueryCustomerInfoDetailResponse response = new QueryCustomerInfoDetailResponse();

        Integer id = request.getId();
        CustomerInfo customerInfo = customerDal.selectById(id);

        //会员等级
        MemberGradeInfo memberGradeInfo = memberGradeDal.selectById(customerInfo.getMemberGradeId());
        response.setMemberGrade(memberGradeInfo.getName());
        response.setMemberGradeId(memberGradeInfo.getId());
        response.setMemberRight(memberGradeInfo.getMemberRight());

        //标签
        CustomerTagParam customerTagParam = new CustomerTagParam();
        customerTagParam.setCustomerId(customerInfo.getId());
        List<CustomerTagInfo> customerTagList = customerTagDal.selectByExample(customerTagParam);
        //标签名
        if(!CollectionUtil.isEmpty(customerTagList)){
            List<Integer> tagIdList = new ArrayList<>();
            for(CustomerTagInfo customerTagInfo : customerTagList){
                tagIdList.add(customerTagInfo.getTagId());
            }

            TagParam tagParam = new TagParam();
            tagParam.setIdList(tagIdList);
            List<TagInfo> tagInfoList = tagDal.selectByExample(tagParam);
            if(!CollectionUtil.isEmpty(tagInfoList)) {
                List<CustomerTagItem> tagList = new ArrayList<>();
                for(TagInfo tagInfo : tagInfoList){
                    CustomerTagItem item = new CustomerTagItem();
                    item.setTagId(tagInfo.getId());
                    item.setTagName(tagInfo.getName());
                    tagList.add(item);
                }
                response.setTagList(tagList);
            }
        }

        CustomerOrderDto customerOrderDto = orderDal.selectCustomerOrder(customerInfo.getId());

        makeResponse(customerInfo, customerOrderDto, response);

        return response;
    }

    private void makeResponse(CustomerInfo customerInfo, CustomerOrderDto customerOrderDto, QueryCustomerInfoDetailResponse response){
        response.setAvailableCoupon(customerInfo.getAvailableCoupon());
        response.setAvailableScores(customerInfo.getAvailableScores());
        response.setBeCustomerTime(customerInfo.getBeCustomerTime());
        response.setBirthDate(customerInfo.getBirthDate());
        response.setGarmentSize(customerInfo.getGarmentSize());
        response.setGraduateDate(customerInfo.getGraduateDate());
        response.setHobby(customerInfo.getHobby());
        response.setIdentity(customerInfo.getIdentity());
        response.setLastInvite(customerInfo.getLastInvite());
        response.setLiveAddress(customerInfo.getLiveAddress());
        response.setLiveProvince(customerInfo.getLiveProvince());
        response.setLiveCountry(customerInfo.getLiveCountry());
        response.setLiveArea(customerInfo.getLiveArea());
        response.setLiveCity(customerInfo.getLiveCity());
        response.setMail(customerInfo.getMail());
        response.setMajor(customerInfo.getMajor());
        response.setMobile(customerInfo.getMobile());
        response.setName(customerInfo.getName());
        response.setNick(customerInfo.getWechatNick());
        response.setNote(customerInfo.getNote());
        response.setOccupation(customerInfo.getOccupation());
        response.setSchool(customerInfo.getSchool());
        response.setSex(customerInfo.getSex());
        response.setSource(customerInfo.getSource());
        response.setWechatNumber(customerInfo.getWechatNumber());
        response.setWorkIndustry(customerInfo.getWorkIndustry());
        response.setLastOrderTime(customerOrderDto.getLastOrderTime());
        response.setTotalConsumeAmount(CommonUtil.fenToYuan(customerOrderDto.getTotalConsumeAmount()).toString());
        response.setTotalConsumeOrder(customerOrderDto.getTotalConsumeOrder());
        response.setTotalRefundAmount(CommonUtil.fenToYuan(customerOrderDto.getTotalRefundAmount()).toString());
        response.setTotalRefundOrder(customerOrderDto.getTotalRefundOrder());
        if(customerOrderDto.getTotalConsumeOrder() > 0) {
            BigDecimal guestPrice = CommonUtil.fenToYuan(customerOrderDto.getTotalConsumeAmount()).divide(new BigDecimal(customerOrderDto.getTotalConsumeOrder()), 2, BigDecimal.ROUND_HALF_DOWN);
            response.setGuestPrice(guestPrice.toString());
        }
    }
}
